package junit.junit;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Stack;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;


//主窗口
public class CalFrame extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textField1;//算式文本域
	private JTextField textField2;//结果文本域
	
	private JButton MC = new JButton("MC");
	private JButton M = new JButton("M");
	private JButton MR = new JButton("MR");
	private JButton per = new JButton("%");
	private JButton AC = new JButton("AC");
	private JButton number0 = new JButton("0");
	private JButton number1 = new JButton("1");
	private JButton number2 = new JButton("2");
	private JButton number3 = new JButton("3");
	private JButton number4 = new JButton("4");
	private JButton number5 = new JButton("5");
	private JButton number6 = new JButton("6");
	private JButton number7 = new JButton("7");
	private JButton number8 = new JButton("8");
	private JButton number9 = new JButton("9");
	private JButton add = new JButton("+");
	private JButton sub = new JButton("-");
	private JButton mul = new JButton("×");
	private JButton div = new JButton("÷");
	private JButton backspace = new JButton("<html>Back<br>space</html>");
	private JButton bracket = new JButton("()");
	private JButton prior = new JButton("<html>Last<br>result</html>");
	private JButton negative = new JButton("+/-");
	private JButton point = new JButton(".");
	private JButton result = new JButton("=");
	
	Font font1 = new Font("Arial", Font.PLAIN, 20);
	Font font2 = new Font("Arial", Font.PLAIN, 25);
	Font font3 = new Font("Arial", Font.PLAIN, 30);
	Font font4 = new Font("Arial", Font.PLAIN, 40);
	Font font5 = new Font("Arial", Font.PLAIN, 50);
	
	private void text()
	{
		textField1 = new JTextField(25);
		textField2 = new JTextField(25);
		textField1.setFont(font1);
		textField2.setFont(font1);
		textField1.setPreferredSize(new Dimension(400, 60));
		textField2.setPreferredSize(new Dimension(400, 60));
		textField1.setHorizontalAlignment(SwingConstants.RIGHT);
		textField2.setHorizontalAlignment(SwingConstants.RIGHT);
		textField1.setEditable(false);
		textField2.setEditable(false);
	}
	
	private void button_ui()
	{
		
				textField1.setBackground(Color.WHITE);
				textField2.setBackground(Color.WHITE);
				MC.setBackground(Color.BLACK);
				MC.setForeground(Color.WHITE);
				M.setBackground(Color.BLACK);
				M.setForeground(Color.WHITE);
				MR.setBackground(Color.BLACK);
				MR.setForeground(Color.WHITE);
				backspace.setBackground(Color.BLACK);
				backspace.setForeground(Color.WHITE);
				AC.setBackground(Color.BLACK);
				AC.setForeground(Color.WHITE);
				number0.setBackground(Color.WHITE);
				number0.setForeground(Color.BLACK);
				number1.setBackground(Color.WHITE);
				number1.setForeground(Color.BLACK);
				number2.setBackground(Color.WHITE);
				number2.setForeground(Color.BLACK);
				number3.setBackground(Color.WHITE);
				number3.setForeground(Color.BLACK);
				number4.setBackground(Color.WHITE);
				number4.setForeground(Color.BLACK);
				number5.setBackground(Color.WHITE);
				number5.setForeground(Color.BLACK);
				number6.setBackground(Color.WHITE);
				number6.setForeground(Color.BLACK);
				number7.setBackground(Color.WHITE);
				number7.setForeground(Color.BLACK);
				number8.setBackground(Color.WHITE);
				number8.setForeground(Color.BLACK);
				number9.setBackground(Color.WHITE);
				number9.setForeground(Color.BLACK);
				negative.setBackground(Color.WHITE);
				negative.setForeground(Color.BLACK);
				point.setBackground(Color.WHITE);
				point.setForeground(Color.BLACK);
				add.setBackground(new Color(200, 200, 200));
				add.setForeground(Color.WHITE);
				sub.setBackground(new Color(200, 200, 200));
				sub.setForeground(Color.WHITE);
				mul.setBackground(new Color(200, 200, 200));
				mul.setForeground(Color.WHITE);
				div.setBackground(new Color(200, 200, 200));
				div.setForeground(Color.WHITE);
				per.setBackground(new Color(150, 150, 150));
				per.setForeground(Color.WHITE);
				bracket.setBackground(new Color(150, 150, 150));
				bracket.setForeground(Color.WHITE);
				prior.setBackground(new Color(150, 150, 150));
				prior.setForeground(Color.WHITE);
				result.setBackground(new Color(255, 150, 50));
				result.setForeground(Color.WHITE);
				//设置按钮字体
				MC.setFont(font2);
				M.setFont(font2);
				MR.setFont(font2);
				per.setFont(font2);
				AC.setFont(font2);
				number0.setFont(font3);
				number1.setFont(font3);
				number2.setFont(font3);
				number3.setFont(font3);
				number4.setFont(font3);
				number5.setFont(font3);
				number6.setFont(font3);
				number7.setFont(font3);
				number8.setFont(font3);
				number9.setFont(font3);
				add.setFont(font4);
				sub.setFont(font5);
				mul.setFont(font4);
				div.setFont(font4);
				backspace.setFont(font1);
				bracket.setFont(font2);
				prior.setFont(font1);
				negative.setFont(font3);
				point.setFont(font4);
				result.setFont(font5);
	}
	
	private void set_false()
	{
		MC.setFocusable(false);
		M.setFocusable(false);
		MR.setFocusable(false);
		per.setFocusable(false);
		AC.setFocusable(false);
		number0.setFocusable(false);
		number1.setFocusable(false);
		number2.setFocusable(false);
		number3.setFocusable(false);
		number4.setFocusable(false);
		number5.setFocusable(false);
		number6.setFocusable(false);
		number7.setFocusable(false);
		number8.setFocusable(false);
		number9.setFocusable(false);
		add.setFocusable(false);
		sub.setFocusable(false);
		mul.setFocusable(false);
		div.setFocusable(false);
		backspace.setFocusable(false);
		bracket.setFocusable(false);
		prior.setFocusable(false);
		negative.setFocusable(false);
		point.setFocusable(false);
		result.setFocusable(false);
	}
	
	private void addAL()
	{
		MC.addActionListener(new ButtonAction());
		M.addActionListener(new ButtonAction());
		MR.addActionListener(new ButtonAction());
		per.addActionListener(new ButtonAction());
		AC.addActionListener(new ButtonAction());
		number0.addActionListener(new ButtonAction());
		number1.addActionListener(new ButtonAction());
		number2.addActionListener(new ButtonAction());
		number3.addActionListener(new ButtonAction());
		number4.addActionListener(new ButtonAction());
		number5.addActionListener(new ButtonAction());
		number6.addActionListener(new ButtonAction());
		number7.addActionListener(new ButtonAction());
		number8.addActionListener(new ButtonAction());
		number9.addActionListener(new ButtonAction());
		add.addActionListener(new ButtonAction());
		sub.addActionListener(new ButtonAction());
		mul.addActionListener(new ButtonAction());
		div.addActionListener(new ButtonAction());
		backspace.addActionListener(new ButtonAction());
		bracket.addActionListener(new ButtonAction());
		prior.addActionListener(new ButtonAction());
		negative.addActionListener(new ButtonAction());
		point.addActionListener(new ButtonAction());
		result.addActionListener(new ButtonAction());
	}
	
	CalFrame()
	{
		setSize(420,470);
		setResizable(false);//不可改变大小
		setLocationRelativeTo(null);

		//文本域的创建和设置
		text();
		
		//按钮面板
		JPanel panel = new JPanel(new GridLayout(5,5));
		panel.setPreferredSize(new Dimension(400, 300));
		
		//美化按钮
		button_ui();
		
		//防止按钮变为焦点而使键盘触发事件失灵
		set_false();
		
		//按钮添加监听器
		addAL();

		//将按钮添加进面板
		panel.add(MC);
		panel.add(M);
		panel.add(MR);
		panel.add(AC);
		panel.add(backspace);
		panel.add(number7);
		panel.add(number8);
		panel.add(number9);
		panel.add(div);
		panel.add(per);
		panel.add(number4);
		panel.add(number5);
		panel.add(number6);
		panel.add(mul);
		panel.add(bracket);
		panel.add(number1);
		panel.add(number2);
		panel.add(number3);
		panel.add(sub);
		panel.add(prior);
		panel.add(negative);
		panel.add(number0);
		panel.add(point);
		panel.add(add);
		panel.add(result);
		
		//布置窗口
		setLayout(new FlowLayout());
		add(textField1);
		add(textField2);
		add(panel);
		setFocusable(true);//窗口获得焦点
		
		//加入临时按键监听器
		addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				//根据按键触发
				switch (e.getKeyCode())
				{
				case KeyEvent.VK_ENTER://回车触发等号
					result.doClick();
					break;
				case KeyEvent.VK_MINUS://减号
					sub.doClick();
					break;
				case KeyEvent.VK_SLASH://正斜杠触发除号
					div.doClick();
					break;
				case KeyEvent.VK_PERIOD://句号触发小数点
					point.doClick();
					break;
				case KeyEvent.VK_BACK_SPACE:
					backspace.doClick();
					break;
				case KeyEvent.VK_DELETE://delete触发AC
					AC.doClick();
					break;
				}
				
				//根据键入内容触发
				switch (e.getKeyChar())
				{
				case '%':
					per.doClick();
					break;
				case '(':
					bracket.doClick();
					break;
				case ')':
					bracket.doClick();
					break;
				case '*':
					mul.doClick();
					break;
				case '=':
					result.doClick();
					break;
				case '+':
					add.doClick();
					break;
				case '0':
					number0.doClick();
					break;
				case '1':
					number1.doClick();
					break;
				case '2':
					number2.doClick();
					break;
				case '3':
					number3.doClick();
					break;
				case '4':
					number4.doClick();
					break;
				case '5':
					number5.doClick();
					break;
				case '6':
					number6.doClick();
					break;
				case '7':
					number7.doClick();
					break;
				case '8':
					number8.doClick();
					break;
				case '9':
					number9.doClick();
					break;
				}
				
			}
		});
	}
	
	
	
	private static boolean p = true;//小数点参数     假为已有小数点
	private static boolean zero = true;//零参数     假为0
	private static boolean finish = false;//完成参数     真为已完成运算
	private static int b = 0;//未匹配括号数
	private static String l = "";//上次结果
	private static String m = "";//储存器
	String formula = "";//获取算式
	char last = ' ';//初始化最后一个字符
	
	public void back_space()
	{
		before();
		if (finish)//已完成处理
		{
			last = formula.charAt(formula.length() - 1);
			textField2.setText("");
			finish = false;
		}
		if (!formula.isEmpty())
		{
			if (last == ' ')//上一个字符为加减乘除运算符
			{
				formula = formula.substring(0, formula.length() - 3);
				textField1.setText(formula);
			}
			else
			{
				formula = formula.substring(0, formula.length() - 1);
              textField1.setText(formula);
			}
			if (last == '(')//左括号删除
			{
				b--;
			}
			if (last == ')')//右括号删除
			{
				b++;
			}
			if (last == '0')//0删除
			{
				zero = true;
			}
			
			//矫正小数点参数和零参数
			if (!formula.isEmpty())
			{
				last = formula.charAt(formula.length() - 1);
				if (last == '.' || (last >= '0' && last <= '9'))
				{
					check();
				}
			}
			
		}
	}
	
	public void AC()
	{
		before();
		if (finish)//完成时处理
		{
			finish = false;
		}
		textField1.setText("");
		textField2.setText("");
		p = true;
		zero = true;
		b = 0;
	}
	
	public void result()
	{
		before();
		if (finish)//完成时处理
		{
			textField2.setText("");
			finish = false;
		}
		if (b == 0)
		{
			
			l = calculate(textField1.getText());//调用计算方法
			if (l.charAt(l.length() - 2) == '.' && l.charAt(l.length() - 1) == '0')//整数时去掉小数点
				l = l.substring(0, l.length() - 2);
			textField2.setText(l);
			if (l == "ERROR")//当结果错误时,上次结果记为空
				l = "";
			finish = true;
			p = true;
			zero = true;
			b = 0;
		}
		else//缺少括号
		{
			textField2.setText("ERROR");
		}
	}
	
	public void prior()
	{
		before();
		if (finish)//完成时处理
		{
			textField1.setText("");
			textField2.setText("");
			formula = "";
			last = ' ';
			finish = false;
		}
		if (!formula.isEmpty())
		{
			//替换最后一个数字
			int i = formula.length() - 1;
			while (i >= 0)
			{
				if (formula.charAt(i) == ' ' || formula.charAt(i) == '(' || formula.charAt(i) == ')')
					break;
				i--;
			}
			i++;
			formula = formula.substring(0, i);
			textField1.setText(formula);
			if (!formula.isEmpty())
				last = formula.charAt(formula.length() - 1);
			else
				last = ' ';
			if (last == ')')//相邻为括号是加上乘号
				addFormula(" × ");
		}
		
		addFormula(l);
	}
	
	public void bracket()
	{
		before();
		if (finish)//完成时处理
		{
			textField1.setText("");
			textField2.setText("");
			finish = false;
		}
		if (b != 0 && (last == ')' || last == '%' || (last >= '0' && last <= '9')))//右括号判断
		{
			addFormula(")");
			b--;
			zero = true;
			p = true;
		}
		else if (last == ' ' || last == '(')//左括号判断
		{
			addFormula("(");
			b++;
		} 
		else if ((last >= '0' && last <= '9') || last == '%' || last == ')')//相邻为括号或数字时是加乘号
		{
			addFormula(" × (");
			b++;
		}
		else if (last == '-')//前面为负号时的处理
		{
			addFormula("1 × (");
			b++;
		}
	}
	
	public void basic_operator(String op)
	{
		before();
		if (finish)//完成时处理
		{
			textField1.setText("");
			textField2.setText("");
			addFormula(l);
			addFormula(op);
			zero = true;
			p = true;
			finish = false;
		}
		else if (!finish && last != '.' && last != '-' && last != '(')//非完成时处理
		{
			if (last == ' ')
			{
				formula = formula.substring(0, formula.length() - 3);
				textField1.setText(formula);
			}
			addFormula(op);
			zero = true;
			p = true;
		}
	}
	
	public void per()
	{
		before();
		if (finish)//完成时处理
		{
			textField1.setText("");
			textField2.setText("");
			addFormula(l);
			addFormula("%");
			finish = false;
		}
		
		if (!finish&& (last >= '0' && last <= '9' || last == ')'))//非完成时处理
		{
			if (last == ')')
			addFormula(" × 100");
			addFormula("%");
		}
	}
	
	public void num(String n)
	{
		before();
		if (finish)//完成时处理
		{
			textField1.setText("");
			textField2.setText("");
			last = ' ';
			finish = false;
		}
		if (last == ')')//相邻为括号是加乘号
			addFormula(" × ");
		addFormula(n);
	}
	
	public void point()
	{
		before();
		if (!finish)
		{
			addFormula(".");
			p = false;
			zero = true;
		}
	}
	
	public void mul_point()
	{
		before();
		if (finish)//完成时处理
		{
			textField1.setText("");
			textField2.setText("");
			last = ' ';
			finish = false;
		}
		if (last == ')')//相邻为括号时加乘号
			addFormula(" × ");
		addFormula("0.");
		p = false;
	}
	
	public void nega()
	{
		before();
		if (finish)//完成时处理
		{
			textField1.setText("");
			textField2.setText("");
			last = ' ';
		}
		if (last == ')')//相邻为括号时加乘号
			addFormula(" × ");
		addFormula("-");
		if (finish)//完成时处理
		{
			addFormula(l);
			finish = false;
		}
	}
	
	public void M()
	{
		before();
		if (m == "")//为空时存储
		{
			//避免存入非数字
			if(!textField2.getText().isEmpty() && textField2.getText().charAt(0) != 'E' && textField2.getText().charAt(0) != 'M')
			{
				m = textField2.getText();
				textField2.setText("M = " + m);
			}
			else//结果文本域为空时存入算式文本域最后一个数字
			{
				
				if (!formula.isEmpty())//为空时不存储
				{
					int i = formula.length() - 1;
					while (i >= 0 && formula.charAt(i) != ' ' && formula.charAt(i) != '(' && formula.charAt(i) != ')')
					{
						i--;
					}
					i++;
					m = formula.substring(i, formula.length());
					textField2.setText("M = " + m);
				}
			}
		}
		else//非空时取出
		{
			if (finish)//完成时处理
			{
				textField1.setText("");
				textField2.setText("");
				formula = "";
				last = ' ';
				finish = false;
			}
			if (!formula.isEmpty())
			{
				//替换最后一个数字
				int i = formula.length() - 1;
				while (i >= 0)
				{
					if (formula.charAt(i) == ' ' || formula.charAt(i) == '(' || formula.charAt(i) == ')')
						break;
					i--;
				}
				i++;
				formula = formula.substring(0, i);
				textField1.setText(formula);
				if (!formula.isEmpty())
					last = formula.charAt(formula.length() - 1);
				else
					last = ' ';
				if (last == ')')//相邻为括号时加乘号
					addFormula(" × ");
			}
			addFormula(m);
		}
	}
	
	public void before()
	{
		formula = textField1.getText();//获取算式
		last = ' ';//初始化最后一个字符
		
		//获取最后一个字符
		if(!formula.isEmpty() && !finish)
		{
			last = formula.charAt(formula.length() - 1);
		}
	}
	
	private class ButtonAction implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			before();
			
			//退格按钮
			if(e.getSource() == backspace)
			{
				back_space();
			}
			//AC按钮
			if(e.getSource() == AC)
			{
				AC();
			}
			
			//等号按钮
			if(e.getSource() == result && last != ' ' && last != '-' && last != '.' && last != '(')
			{
				result();
			}
			
			//上次结果按钮
			if(e.getSource() == prior)
			{
				prior();
			}
			
			//括号按钮
			if(e.getSource() == bracket)
			{
				bracket();
			}
			
			//加号按钮
			if(e.getSource() == add)
			{
				basic_operator(" + ");
			}
			
			//减号按钮
			if(e.getSource() == sub)
			{
				basic_operator(" - ");
			}
			
			//乘号按钮
			if(e.getSource() == mul)
			{
				basic_operator(" × ");
			}
			
			//除号按钮
			if(e.getSource() == div)//完成时处理
			{
				basic_operator(" ÷ ");
			}
			
			//百分号
			if(e.getSource() == per)
			{
				per();
			}
			
			//数字0-9按钮
			if(e.getSource() == number0 && zero && last != '%')
			{
				num("0");
				if (last == ' ' || last == '-')//为0时禁止继续输入数字
					zero = false;
			}
			if(e.getSource() == number1 && last != '%' && zero)
			{
				num("1");
			}
			if(e.getSource() == number2 && last != '%' && zero)
			{
				num("2");
			}
			if(e.getSource() == number3 && last != '%' && zero)
			{
				num("3");
			}
			if(e.getSource() == number4 && last != '%' && zero)
			{
				num("4");
			}
			if(e.getSource() == number5 && last != '%' && zero)
			{
				num("5");
			}
			if(e.getSource() == number6 && last != '%' && zero)
			{
				num("6");
			}
			if(e.getSource() == number7 && last != '%' && zero)
			{
				num("7");
			}
			if(e.getSource() == number8 && last != '%' && zero)
			{
				num("8");
			}
			if(e.getSource() == number9 && last != '%' && zero)
			{
				num("9");
			}
			
			//小数点按钮
			if(e.getSource() == point && p && (last >= '0' && last <= '9'))//当前面有数字时
			{
				point();
				
			}
			if(e.getSource() == point && p && last != '%')//当前面没有数字时
			{
				mul_point();
			}
			
			//负号按钮
			if(e.getSource() == negative && (last == ' ' || last == '(' || last == ')'))
			{
				nega();
			}
			
			//MC按钮
			if(e.getSource() == MC)
			{
				m = "";
			}
			
			//MR按钮
			if(e.getSource() == MR)
			{
				
				textField2.setText("M = " + m);
			}
			
			//M按钮
			if(e.getSource() == M)
			{
				M();
			}
		}
	}
	
	//校正参数方法
	private void check()
	{
		String formula = textField1.getText();
		p = true;
		zero = true;
		int n = 0;
		for (int i = 1; formula.charAt(formula.length() - i) != '-' && formula.charAt(formula.length() - i) != ' ' && formula.charAt(formula.length() - i) != '(' && i < formula.length(); i++)
		{
			//校正小数点参数
			if (formula.charAt(formula.length() - i) == '.')
				p = false;
			n++;
			
		}
		if (n == 1 && formula.charAt(formula.length() - 1) == '0')
		{
			//校正零参数
			zero = false;
		}
	}
	
	//在算式文本域添加内容
	void addFormula(String add)
	{
		String formula = textField1.getText();
		textField1.setText(formula.concat(add));
	}
	
	//计算方法
	public String calculate(String Test)
	{
		//前后加#号
		String formula = "#";
		formula = formula.concat(Test);
		formula = formula.concat("#");
		
		//临时变量以及数字栈和运算符栈
		double num = 0.0;
		Stack<Double> doubleStack = new Stack<Double>();
		int n = 0;
		Stack<Integer> intStack = new Stack<Integer>();
		/* 根据各运算符优先级进行编号储存,便于判断优先级
		 * 0 = #
		 * 2 = (
		 * 4 = )
		 * 6 = +
		 * 7 = -
		 * 8 = ×
		 * 9 = ÷
		 */
		
		//第一个运算符#号入栈
		intStack.push(0);
		for (int i = 1; i < formula.length(); i++)
		{
			//数字入栈
			if (formula.charAt(i) == '-' || (formula.charAt(i) >= '0' && formula.charAt(i) <= '9'))
			{
				int j;
				for (j = i; formula.charAt(j) != ' ' && formula.charAt(j) != '%' && formula.charAt(j) != ')' && formula.charAt(j) != '#'; j++)
				{}
				num = Double.parseDouble(formula.substring(i, j));
				doubleStack.push(num);
				i = j - 1;
			}
			//百分号运算
			else if (formula.charAt(i) == '%')
			{
				num = (Double) doubleStack.pop();
				doubleStack.push(num / 100);
			}
			//加减乘除入栈
			else if (formula.charAt(i) == ' ')
			{
				i++;
				//获取编码
				switch (formula.charAt(i))
				{
				case '+':
					n = 6;
					break;
				case '-':
					n = 7;
					break;
				case '×':
					n = 8;
					break;
				case '÷':
					n = 9;
					break;
				}
				while (true)
				{
					if ((Integer) intStack.peek() == 4)//当前一个运算符是右括号时直接去掉这对括号
					{
						intStack.pop();
						intStack.pop();
					}
					else if (n / 2 > (Integer) intStack.peek() / 2)//当优先级高于上一个运算符时入栈并调取下一个
					{
						intStack.push(n);
						break;
					}
					else//当优先级等于或小于上一个运算符时,执行上一个运算符
					{
						int operator = (Integer) intStack.pop();
						if (operator == 0)
							return String.valueOf((Double) doubleStack.pop());//检测到计算结束,返回结果
						try
						{
							doubleStack.push(basic_calc(operator, doubleStack));
						}
						catch (ArithmeticException ex)
						{
							return "ERROR";
						}
					}
				}
				i++;
			}
			else//其他运算符入栈
			{
				//获取编码
				switch (formula.charAt(i))
				{
				case '#':
					n = 0;
					break;
				case '(':
					n = 2;
					break;
				case ')':
					n = 4;
					break;
				}
				while (true)
				{
					if ((Integer) intStack.peek() == 4)//当前一个运算符是右括号时直接去掉这对括号
					{
						intStack.pop();
						intStack.pop();
					}
					else if (n / 2 > (Integer) intStack.peek() / 2)//当优先级高于上一个运算符时入栈并调取下一个
					{
						intStack.push(n);
						break;
					}
					else if (n == 2)//当要入栈的是个左括号直接入栈
					{
						intStack.push(n);
						break;
					}
					else//当前一个是加减乘除时,执行上一个运算符
					{
						int operator = (Integer) intStack.pop();
						if (operator == 0)
							return String.valueOf((Double) doubleStack.pop());//检测到计算结束,返回结果
						try
						{
							doubleStack.push(basic_calc(operator, doubleStack));
						}
						catch (ArithmeticException ex)
						{
							return "ERROR";
						}
					}
				}
			}
			
		}
		return String.valueOf((Double) doubleStack.pop());//返回结果
	}
	
	public double basic_calc(int operator, Stack<Double> doubleStack)
	{
		double num, num1;
		switch (operator)
		{
		case 6:
			num = (Double) doubleStack.pop();
			num1 = (Double) doubleStack.pop();
			return num + num1;
		case 7:
			num = (Double) doubleStack.pop();
			num1 = (Double) doubleStack.pop();
			return num1 - num;
		case 8:
			num = (Double) doubleStack.pop();
			num1 = (Double) doubleStack.pop();
			return num * num1;
		case 9:
			num = (Double) doubleStack.pop();
			num1 = (Double) doubleStack.pop();
			if (num == 0)
				throw new ArithmeticException("Divisor cannot be zero");
			return num1 / num;
		default:
			return 0;
		}
	}
	
	public String getResult()
	{
		return textField2.getText();
	}
}


